package com.example.statistics.net.callback

import com.example.statistics.net.DataResult
import com.jhj.httplibrary.adapt.CallAdapt
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type

abstract class DataHttpCallAdapter<T> : CallAdapt<DataResult<T>>() {

    val type: Type
        get() {
            //获取当前类带有泛型的父类
            val clazz = this.javaClass.genericSuperclass
            val type = if (clazz is ParameterizedType) {
                //获取父类的泛型参数（参数可能有多个，获取第一个）
                clazz.actualTypeArguments[0]
            } else {
                clazz
            }
            return type(DataResult::class.java, type)
        }


    fun type(raw: Class<*>, vararg args: Type?): ParameterizedType {
        return object : ParameterizedType {
            override fun getRawType(): Type {
                return raw
            }

            override fun getActualTypeArguments(): Array<out Type?> {
                return args
            }

            override fun getOwnerType(): Type? {
                return null
            }
        }
    }

}